<?php
/**
 * Descript
 * User: blue
 * Date: 2018/8/5 14:16
 */

namespace app\lib\serverLib;


class Curl
{
    private $ch;
    public function __construct()
    {
        if(!$this->ch){
            echo 'cosntruct';
            $this->ch=curl_init();
        }

    }

    /**
     * @param $url
     * @param string $method
     * @param null $data
     * @param int $http
     * @return mixed
     */
    public function curlRequest($url,$method="get",$data=null,$http=1){
        $this->curlSetOpt();
        curl_setopt($this->ch,CURLOPT_URL,$url);
        if($http){
            curl_setopt($this->ch,CURLOPT_SSL_VERIFYHOST,false);
            curl_setopt($this->ch,CURLOPT_SSL_VERIFYPEER,false);
        }
        if(strtolower($method)=="post"){
            curl_setopt($this->ch,CURLOPT_POST,true);
            curl_setopt($this->ch,CURLOPT_POSTFIELDS,$data);
        }
        $str=curl_exec($this->ch);
        return $str;
    }

    /**
     * 设置curl的属性
     */
    protected function curlSetOpt(){
        curl_setopt($this->ch,CURLOPT_HEADER,false);
        curl_setopt($this->ch,CURLOPT_CONNECTTIMEOUT,10);
        //设置了该选项,成功后返回执行结果.不设置返回true.
        curl_setopt($this->ch,CURLOPT_RETURNTRANSFER,true);
    }

    // public function __destruct()
    // {
    //     if($this->ch){
    //         echo 'destruct';
    //         curl_close($this->ch);
    //     }
    // }

}